﻿#include "OperatorsNew.h"
#include "GUIFrame/MainWindow.h"
#include "GUIFrame/CentralWidget.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"

#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGroupData.h"

#include "FITK_Component/FITKVAOneXMLIO/FITKVAOneXMLIOInterface.h"

#include "OperatorsInterface/GraphEventOperator.h"
#include "OperatorsInterface/GraphPostEventOperator.h"
#include "updateBrowserTable.h"
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
namespace ModelOper
{
    OperatorsNew::OperatorsNew()
    {

    }

    OperatorsNew::~OperatorsNew()
    {

    }

    bool OperatorsNew::execGUI()
    {
        //主界面
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //获取工作目录
        QString wk;
        auto settings = FITKAPP->getAppSettings();
        if (settings)
            wk = settings->getWorkingDir();
        // QString fileName = QFileDialog::getOpenFileName(mw, QObject::tr("Open INP File"),"" , "INP(*.inp)");

        QFileDialog dlg(mw, QObject::tr("Export VA1 File"), wk, "VA1(*.va1)");
        dlg.setAcceptMode(QFileDialog::AcceptSave);  //保存模式
        dlg.show();
        //阻塞，否则键盘事件处理会出问题
        bool accept = false;
        QEventLoop loop;
        connect(&dlg, &QFileDialog::accepted, [&] {loop.quit(); accept = true;  });
        connect(&dlg, &QFileDialog::rejected, [&] {loop.quit(); accept = false;  });
        loop.exec();

        if (!accept) return false;
        QStringList files = dlg.selectedFiles(); //选择的文件名称
        if (files.isEmpty()) return false;
        QString fileName = files.at(0);

        if (fileName.isEmpty())
            return false;
        //判断是否缺失扩展名
        QString fileExtension = ".va1";
        if (!fileName.contains(fileExtension)) fileName.append(fileExtension);

        if (fileName.isEmpty())
            return false;
        //存储信息
        if (_emitter == nullptr) return false;

        this->setArgs("FileName", fileName);
        return true;
    }

    bool OperatorsNew::execProfession()
    {
        //导入文件名
        QString fileName;
        if (!this->argValue<QString>("FileName", fileName) || fileName.isEmpty()) return false;
        clearAll();

        //导入默认文件(指定文件路径)
        QString path = FITKAPP->applicationDirPath() + "\\defaultXML.xml";
        VAOne::FITKVAOneXMLIOInterface* vaone = dynamic_cast<VAOne::FITKVAOneXMLIOInterface*>(FITKAPP->getComponents()->getComponentByName("VAOneXMLIO"));
        if (!vaone) return false;
        //获取要存储的数据位置
        Core::FITKAbstractDataObject* model =
            FITKAPP->getGlobalData()->getMeshData<Core::FITKAbstractDataObject>();
        Core::FITKAbstractDataObject* physics =
            FITKAPP->getGlobalData()->getPhysicsData<Core::FITKAbstractDataObject>();
        //传入参数读取xml文件
        vaone->setDataObject("Model", model);
        vaone->setDataObject("Physics", physics);
        vaone->setFileName(path);
        vaone->runInThread(false);
        vaone->exec(1);
        bool b = vaone->isSuccess();
        //初始化文件读取失败
        if (!b)
        {
            QMessageBox::warning(nullptr, ("warning"), ("Failed to load initialization file, program exits!"));
        }

        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper)
        {
            graphOper->updateAll(true);
        }

        EventOper::GraphPostEventOperator* postOper = FITKOPERREPO->getOperatorT<EventOper::GraphPostEventOperator>("GraphPost");
        if (postOper)
        {
            postOper->disablePostData();
        }
        
        //更新表格
        ModelOper::updateBrowserTable();
        //保存执行的脚本
        QString scr = QString("Global.Files.NewFile('%1')").arg(fileName);
        this->saveScript(scr);

        return true;
    }

    void OperatorsNew::clearAll()
    {
    //清空GeometryData
        Acoustics::FITKAcousticsGeometryData* geoData = Acoustics::FITKAcousticsGeometryData::GetGeometryDataFromFrame();
        if (geoData) {
            geoData->clear();
        }
    //清空离散模型
        Acoustics::FITKAcousticsDiscModel* disc = Acoustics::FITKAcousticsDiscModel::GetDiscModelFromFrame();
        disc->clearDiscModel();
    //清空物理属性管理器
        Acoustics::FITKAcousticsPhysics* physics = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (physics) {
            physics->clearAcousticsPhysics();
        }
    //清空声学分组数据管理器   
        Acoustics::FITKAcousticsGroupManager* groupData =Acoustics::FITKAcousticsGroupManager::GetGroupManagerFromFrame();
        if (groupData)
        {
            groupData->clear();
        }
    }
}

